package com.syq;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;

/**
 * @ClassName: ShiroTest2
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/7/18 14:58
 */


public class ShiroTest2 {
    public static void main(String[] args) {

        Realm realm = new IniRealm("classpath:shiro.ini");

        DefaultSecurityManager securityManager = new DefaultSecurityManager();

        securityManager.setRealm(realm);

        SecurityUtils.setSecurityManager(securityManager);

        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken authenticationToken = new UsernamePasswordToken();
        authenticationToken.setUsername("syq");
        authenticationToken.setPassword("646474".toCharArray());
        try {
            subject.login(authenticationToken);
            System.out.println("登陆成功");
        } catch (AuthenticationException e) {
            e.printStackTrace();
            System.out.println("登陆失败" + e.getMessage());
        }
    }
}